package com.ruoyi.system.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.comment.EngComment;
import com.ruoyi.system.mapper.*;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
//import com.ruoyi.system.mapper.BEEngMapper;
//import com.ruoyi.system.domain.BEEng;
import com.ruoyi.system.service.IBEEngService;




/**
 * 【请填写功能名称】工程 Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-22
 */
@Service
public class BEEngServiceImpl implements IBEEngService 
{
    @Autowired
    private BEEngMapper bEEngMapper;
    @Autowired
    private BEAnjuanMapper bEAnjuanMapper;
    @Autowired
    private BEFileMapper bEFileMapper;
    @Autowired
    private BEAfengMapper bEAfengMapper;
    @Autowired
    private BEAfanjuanMapper bEAfanjuanMapper;
    @Autowired
    private BEAffileMapper bEAffileMapper;


    //private ProjectDataMapper projectDataMapper;


    /**
     * 查询【请填写功能名称】
     * 
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public BEEng selectBEEngById(String id)
    {
        return bEEngMapper.selectBEEngById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param bEEng 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<BEEng> selectBEEngList(BEEng bEEng)
    {
        return bEEngMapper.selectBEEngList(bEEng);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param bEEng 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertBEEng(BEEng bEEng)
    {
        return bEEngMapper.insertBEEng(bEEng);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param bEEng 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateBEEng(BEEng bEEng)
    {
        return bEEngMapper.updateBEEng(bEEng);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBEEngByIds(String[] ids)
    {
        return bEEngMapper.deleteBEEngByIds(ids);
    }




    /**
     * 通过审核
     *
     * @param bEEng 工程导入预览
     * @return 结果
     */
    @Override
    public int approvedEng(BEEng bEEng)
    {

        BEAfeng bEAfeng = moveEng(bEEng);
        int eng=bEAfengMapper.insertBEAfeng(bEAfeng);

        BEAnjuan bEAnjuan = new BEAnjuan();
        bEAnjuan.setEeid(bEAfeng.getId());
        List<BEAnjuan> bEAnjuanList = bEAnjuanMapper.selectBEAnjuanList(bEAnjuan);


        List<BEAfanjuan> bEAfanjuanList = moveVolume(bEAnjuanList);
        for(BEAfanjuan bEAfanjuan : bEAfanjuanList){

            bEAfanjuanMapper.insertBEAfanjuan(bEAfanjuan);

            BEFile bEFile = new BEFile();
            bEFile.setEaid(bEAfanjuan.getId());

            List<BEFile> bEFileList = bEFileMapper.selectBEFileList(bEFile);


            List<BEAffile> bEAffileList = moveFile(bEFileList);
            for(BEAffile bEAffile : bEAffileList){

                bEAffileMapper.insertBEAffile(bEAffile);
            }
        }
        //this.deleteVolumeFile(bEEng.getId());//审核后将原表删除
//        bEEng.setYuLiuB(Long.valueOf(1));//审核后将原表是否审核字段改为1，表示已审核通过。
        bEEngMapper.updateBEEng(bEEng);
        return eng;
    }



    /**
     * 复制工程信息
     */
    public BEAfeng moveEng(BEEng bEEng){
        BEAfeng bEAfeng = new BEAfeng();
        bEAfeng.setId                                       (bEEng.getId());
        bEAfeng.setEngName                                  (bEEng.getEngName());
        bEAfeng.setEngNo                                    (bEEng.getEngNo());
        bEAfeng.setDangAnGuanNo                             (bEEng.getDangAnGuanNo());
        bEAfeng.setYiJiaoDanWei                             (bEEng.getYiJiaoDanWei());
        bEAfeng.setBianZhiDanWei                            (bEEng.getBianZhiDanWei());
        bEAfeng.setBianZhiDateStart                         (bEEng.getBianZhiDateStart());
        bEAfeng.setBianZhiDateEnd                           (bEEng.getBianZhiDateEnd());
        bEAfeng.setEngAddress                               (bEEng.getEngAddress());
        bEAfeng.setEngCost                                  (bEEng.getEngCost());
        bEAfeng.setAnJuanShu                                (bEEng.getAnJuanShu());
        bEAfeng.setDangAnGuanName                           (bEEng.getDangAnGuanName());
        bEAfeng.setShuZiHuaDanWei                           (bEEng.getShuZiHuaDanWei());
        bEAfeng.setProjectStatus                            (bEEng.getProjectStatus());
        bEAfeng.setFileDirectory                            (bEEng.getFileDirectory());
        bEAfeng.setCreationTime                             (bEEng.getCreationTime());
        bEAfeng.setIsNewEng                                 (bEEng.getIsNewEng());
        bEAfeng.setIsHasOtherFile                           (bEEng.getIsHasOtherFile());
        bEAfeng.setYuLiuA                                   (bEEng.getYuLiuA());
        bEAfeng.setYuLiuB                                   (bEEng.getYuLiuB());
        bEAfeng.setYuLiuC                                   (bEEng.getYuLiuC());
        bEAfeng.setYuLiuD                                   (bEEng.getYuLiuD());
        bEAfeng.setYuLiuE                                   (bEEng.getYuLiuE());
        bEAfeng.setEngineeringIdentificationNumber          (bEEng.getEngineeringIdentificationNumber());
        bEAfeng.setUnitConstructionCategory                 (bEEng.getUnitConstructionCategory());
        bEAfeng.setArchiveClassification                    (bEEng.getArchiveClassification());
        bEAfeng.setArchiveCategory                          (bEEng.getArchiveCategory());
        bEAfeng.setBuildingUnit                             (bEEng.getBuildingUnit());
        bEAfeng.setProjectApprovalUnit                      (bEEng.getProjectApprovalUnit());
        bEAfeng.setProjectApprovalNumber                    (bEEng.getProjectApprovalNumber());
        bEAfeng.setPlanningPermitNumber                     (bEEng.getPlanningPermitNumber());
        bEAfeng.setLandPlanningPermitNumber                 (bEEng.getLandPlanningPermitNumber());
        bEAfeng.setLandUsePermitNumber                      (bEEng.getLandUsePermitNumber());
        bEAfeng.setConstructionPermitNumber                 (bEEng.getConstructionPermitNumber());
        bEAfeng.setTotalLength                              (bEEng.getTotalLength());
        bEAfeng.setArchivePageNumber                        (bEEng.getArchivePageNumber());
        bEAfeng.setStartDate                                (bEEng.getStartDate());
        bEAfeng.setCompletionDate                           (bEEng.getCompletionDate());
        bEAfeng.setStartPoint                               (bEEng.getStartPoint());
        bEAfeng.setEndPoint                                 (bEEng.getEndPoint());
        bEAfeng.setExplorationUnit                          (bEEng.getExplorationUnit());
        bEAfeng.setExplorationProjectLeader                 (bEEng.getExplorationProjectLeader());
        bEAfeng.setDesignUnit                               (bEEng.getDesignUnit());
        bEAfeng.setDesignProjectLeader                      (bEEng.getDesignProjectLeader());
        bEAfeng.setRegister                                 (bEEng.getRegister());
        return bEAfeng;
    }


    /**
     * 复制案卷信息
     */
    public List<BEAfanjuan> moveVolume(List<BEAnjuan> bEAnjuanList) {
        List<BEAfanjuan> bEAfanjuanList = new ArrayList<>();
        for (BEAnjuan bEAnjuan : bEAnjuanList) {
            BEAfanjuan bEAfanjuan = new BEAfanjuan();

            bEAfanjuan.setId(bEAnjuan.getId());
            bEAfanjuan.setEeid(bEAnjuan.getEeid());
            bEAfanjuan.setAnJuanName(bEAnjuan.getAnJuanName());
            bEAfanjuan.setFileDirectory(bEAnjuan.getFileDirectory());
            bEAfanjuan.setCreationTime(bEAnjuan.getCreationTime());
            bEAfanjuan.setHenum(bEAnjuan.getHenum());
            bEAfanjuan.setVtype(bEAnjuan.getVtype());
            bEAfanjuan.setTname(bEAnjuan.getTname());
            bEAfanjuan.setBuildorganization(bEAnjuan.getBuildorganization());
            bEAfanjuan.setAmount(bEAnjuan.getAmount());
            bEAfanjuan.setSpec(bEAnjuan.getSpec());
            bEAfanjuan.setFilestart(bEAnjuan.getFilestart());
            bEAfanjuan.setFileend(bEAnjuan.getFileend());
            bEAfanjuan.setSavetime(bEAnjuan.getSavetime());
            bEAfanjuan.setSecuritylayer(bEAnjuan.getSecuritylayer());
            bEAfanjuan.setThematic(bEAnjuan.getThematic());
            bEAfanjuan.setAnnotations(bEAnjuan.getAnnotations());
            bEAfanjuan.setTotalfile(bEAnjuan.getTotalfile());
            bEAfanjuan.setTotalpage(bEAnjuan.getTotalpage());
            bEAfanjuan.setTxtpage(bEAnjuan.getTxtpage());
            bEAfanjuan.setCmen(bEAnjuan.getCmen());
            bEAfanjuan.setCtime(bEAnjuan.getCtime());
            bEAfanjuan.setAssessor(bEAnjuan.getAssessor());
            bEAfanjuan.setAtime(bEAnjuan.getAtime());
            bEAfanjuan.setContent(bEAnjuan.getContent());
            bEAfanjuan.setTopnode(bEAnjuan.getTopnode());
            bEAfanjuan.setInjectionperson(bEAnjuan.getInjectionperson());
            bEAfanjuan.setInjectiondate(bEAnjuan.getInjectiondate());
            bEAfanjuan.setCheckperson(bEAnjuan.getCheckperson());
            bEAfanjuan.setCheckdate(bEAnjuan.getCheckdate());
            bEAfanjuan.setRegprojid(bEAnjuan.getRegprojid());
            bEAfanjuan.setPath(bEAnjuan.getPath());
            bEAfanjuan.setStoreposition(bEAnjuan.getStoreposition());
            bEAfanjuan.setVolumeYuliu1(bEAnjuan.getVolumeYuliu1());
            bEAfanjuan.setVolumeYuliu2(bEAnjuan.getVolumeYuliu2());
            bEAfanjuan.setVolumeYuliu3(bEAnjuan.getVolumeYuliu3());
            bEAfanjuan.setVolumeYuliu4(bEAnjuan.getVolumeYuliu4());

            bEAfanjuan.setCheckupflag(null);
            bEAfanjuan.setOpenflag(null);
            bEAfanjuan.setFinishflag(null);
            bEAfanjuan.setAnjuanLiushui(null);
            bEAfanjuan.setBoxLiushui(null);

            bEAfanjuanList.add(bEAfanjuan);
        }
        return bEAfanjuanList;
    }


    /**
     * 复制文件信息
     */
    public List<BEAffile> moveFile(List<BEFile> bEFileList){
        List<BEAffile> bEAffileList = new ArrayList<>();
        for(BEFile bEFile : bEFileList){
            BEAffile bEAffile = new BEAffile();

            bEAffile.setId(bEFile.getId());    //所有属性一一对应
            bEAffile.setEaid(bEFile.getEaid());
            bEAffile.setAid(bEFile.getAid());
            bEAffile.setWenJianName(bEFile.getWenJianName());
            bEAffile.setPageShu(bEFile.getPageShu());
            bEAffile.setFileDirectory(bEFile.getFileDirectory());
            bEAffile.setState(bEFile.getState());
            bEAffile.setCreationTime(bEFile.getCreationTime());
            bEAffile.setFileId(bEFile.getFileId());
            bEAffile.setEfilenum(bEFile.getEfilenum());
            bEAffile.setListnum(bEFile.getListnum());
            bEAffile.setListId(bEFile.getListId());
            bEAffile.setIndexId(bEFile.getIndexId());
            bEAffile.setVolumeid(bEFile.getVolumeid());
            bEAffile.setOldvolumeid(bEFile.getOldvolumeid());
            bEAffile.setFilename(bEFile.getFilename());
            bEAffile.setDuty(bEFile.getDuty());
            bEAffile.setTagnum(bEFile.getTagnum());
            bEAffile.setFilenum(bEFile.getFilenum());
            bEAffile.setThematic(bEFile.getThematic());
            bEAffile.setCtime(bEFile.getCtime());
            bEAffile.setPagenumber(bEFile.getPagenumber());
            bEAffile.setSecuritylayer(bEFile.getSecuritylayer());
            bEAffile.setFiletype(bEFile.getFiletype());
            bEAffile.setSavetime(bEFile.getSavetime());
            bEAffile.setFiletime(bEFile.getFiletime());
            bEAffile.setFileno(bEFile.getFileno());
            bEAffile.setFileatt(bEFile.getFileatt());
            bEAffile.setCarriertype(bEFile.getCarriertype());
            bEAffile.setTotalnum(bEFile.getTotalnum());
            bEAffile.setTotal(bEFile.getTotal());
            bEAffile.setPapertype(bEFile.getPapertype());
            bEAffile.setBuildorganization(bEFile.getBuildorganization());
            bEAffile.setWcompanyId(bEFile.getWcompanyId());
            bEAffile.setVnum(bEFile.getVnum());
            bEAffile.setIsArchived(bEFile.getIsArchived());
            bEAffile.setProjIndex(bEFile.getProjIndex());
            bEAffile.setComments(bEFile.getComments());
            bEAffile.setAbstracts(bEFile.getAbstracts());
            bEAffile.setKeywords(bEFile.getKeywords());
            bEAffile.setFilepath(bEFile.getFilepath());
            bEAffile.setFileReserved1(bEFile.getFileReserved1());
            bEAffile.setFileReserved2(bEFile.getFileReserved2());
            bEAffileList.add(bEAffile);
        }
        return bEAffileList;
    }






    /**
     * 审核不通过
     *
     * @param bEEng1 工程导入预览
     * @return 结果
     */
    public int noPassEng(BEEng bEEng1){
        String id = bEEng1.getId();
        BEEng bEEng = bEEngMapper.selectBEEngById(id);


        /*File directory = new File(bEEng.getFileDirectory());
        this.delete(directory);
        BEAnjuan bEAnjuan = new BEAnjuan();
        bEAnjuan.setEeid(id);
        List<BEAnjuan> bEAnjuanList = bEAnjuanMapper.selectBEAnjuanList(bEAnjuan);
        for (BEAnjuan bEAnjuan1 : bEAnjuanList){
            BEFile bEFile = new BEFile();
            bEFile.setEaid(bEAnjuan1.getId());
            List<BEFile> bEFileList = bEFileMapper.selectBEFileList(bEFile);
            for(BEFile bEFile1 : bEFileList){
                bEFileMapper.deleteBEFileById(bEFile1.getId());
            }
            bEAnjuanMapper.deleteBEAnjuanById(bEAnjuan1.getId());
        }*/

        // 更新审核前的表的字段 "是否审核" 为 -1 表示未通过，并记录详细信息
        bEEng.setYuLiuB(Long.valueOf(-1)); // 是否审核
        bEEng.setYuLiuE(bEEng1.getYuLiuE()); // 审核不通过的详细信息
        int result = bEEngMapper.updateBEEng(bEEng);


        // 提示用户去处理未完成事项
        System.out.println("审核未通过，请前往页面 a 处理未完成事项。");

        return result;

        //return bEEngMapper.deleteBEEngById(id);
    }




    /**
     *
     * 删除数据库中相应的案卷和文件
     *
     */

    public void deleteVolumeFile(String id){
        BEAnjuan bEAnjuan = new BEAnjuan();
        bEAnjuan.setEeid(id);
        List<BEAnjuan> bEAnjuanList = bEAnjuanMapper.selectBEAnjuanList(bEAnjuan);
        for (BEAnjuan bEAnjuan1 : bEAnjuanList){
            BEFile bEFile = new BEFile();
            bEFile.setEaid(bEAnjuan1.getId());
            List<BEFile> bEFileList = bEFileMapper.selectBEFileList(bEFile);
            for(BEFile bEFile1 : bEFileList){
                bEFileMapper.deleteBEFileById(bEFile1.getId());
            }
            bEAnjuanMapper.deleteBEAnjuanById(bEAnjuan1.getId());

        }
        bEEngMapper.deleteBEEngById(id);

    }





    /**
     *
     * 删除文件夹
     *
     */
    public static void delete(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();

            if (files != null) {
                for (File subFile : files) {
                    delete(subFile);
                }
            }
        }
        file.delete();
    }


    /**
     * 删除【请填写功能名称】信息
     * 
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteBEEngById(String id)
    {
        return bEEngMapper.deleteBEEngById(id);
    }


    /**
     * 查询工程注释信息列表
     *
     * @return 工程信息集合
     */
    public List<EngComment> selectBEEngComInfoList(){
        return bEEngMapper.selectBEEngComInfoList();
    }



}
